package flown.window;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.ChangeListener;
import javax.swing.event.MenuKeyListener;

import flown.window.listener.ActionAndChangeListener;
import flown.window.ui.Widget;
import flown.window.ui.组件;


/**
 * @author flown
 */

public class PopupMenu
{
	private JPopupMenu popup_menu;
	private ArrayList<JMenuItem> menu_item;
	
	public PopupMenu(){this.popup_menu = new JPopupMenu();menu_item = new ArrayList<JMenuItem>();}
	public PopupMenu(String label)
	{
		this.popup_menu = new JPopupMenu(label);
		this.menu_item = new ArrayList<JMenuItem>();
	}
	
	
	/**
	 * 设置菜单可视
	 * @param state true or false
	 * */
	public final void SetVisible(boolean state){this.popup_menu.setVisible(state);}


	/**
	 * 显示菜单
	 * @param invoker Component
	 * @param x 显示横坐标
	 * @param y 显示纵坐标
	 */
	public final void Show(Component invoker, int x, int y){this.popup_menu.show(invoker, x, y);}
	public final void Show(Window window, int x, int y){this.popup_menu.show(window.GetFrame(), x, y);}
	public final void Show(窗口 window, int x, int y){this.popup_menu.show(window.获取窗口(), x, y);}
	public final void Show(Widget widget, int x, int y){this.popup_menu.show(widget.Get(), x, y);}
	public final void Show(组件 widget, int x, int y){this.popup_menu.show(widget.获取(), x, y);}
	
	
	
	/**
	 * 添加菜单监听器
	 * @param listener menu key listener
	 * */
	public final void AddMenuKeyListener(MenuKeyListener listener){this.popup_menu.addMenuKeyListener(listener);}
	
	
	/**
	 * 添加菜单项监听器
	 * @param index 菜单项索引
	 * @param listener action listener
	 * */
	public final void AddActionListener(int index,ActionListener listener)
	{
		this.menu_item.get(index).addActionListener(listener);
	}
	
	/**
	 * 添加菜单项监听器
	 * @param index 菜单项索引
	 * @param listener change listener
	 * */
	public final void AddChangeListener(int index,ChangeListener listener)
	{
		this.menu_item.get(index).addChangeListener(listener);
	}
	
	/**
	 * 添加菜单项监听器
	 * @param index 菜单项索引
	 * @param listener ActionAndChangeListener
	 * */
	public final void AddActionAndChangeListener(int index,ActionAndChangeListener listener)
	{
		this.menu_item.get(index).addActionListener(listener);
		this.menu_item.get(index).addChangeListener(listener);
	}
	
	
	
	/**
	 * 添加一个菜单项
	 * @param text 菜单项文本
	 * @return 菜单项索引
	 * */
	public final int AddItem(String text)
	{
		this.menu_item.add(new JMenuItem(text));
		this.popup_menu.add(this.menu_item.get(this.menu_item.size() - 1));
		return this.menu_item.size() - 1;
	}
	
	/**
	 * 添加n个菜单项
	 * @param text 菜单项文本
	 * @return 菜单项索引数组
	 * */
	public final int[] AddItems(String... text)
	{
		int[] index = new int[text.length];
		for(int i = 0;i < text.length;i++)
			index[i] = this.AddItem(text[i]);
		
		return index;
	}
}






